package com.lepu.app.fun.my.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.MyNetUtil;
import com.core.lib.application.BaseFragmentActivity;
import com.eyzhs.app.R;
import com.lepu.app.fun.my.MyArticleInfoActivity;
import com.lepu.app.fun.my.PubContentDetailActivity;
import com.lepu.app.fun.my.bean.BeanCollectArticle;
import com.lepu.app.fun.my.bean.BeanCollectKnowledge;

public class AdapterCollectArticleList extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private Context mContext = null;
	private ArrayList<BeanCollectArticle> mArrayList = null;

	public AdapterCollectArticleList(Context context,
			ArrayList<BeanCollectArticle> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.my_question_list_item, null);

			holder = new ViewHolder();

			LinearLayout mainItemLayout = (LinearLayout) convertView
					.findViewById(R.id.mainItemLayout);
			TextView titleTextView = (TextView) convertView
					.findViewById(R.id.titleTextView);

			holder.mainItemLayout = mainItemLayout;
			holder.titleTextView = titleTextView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final BeanCollectArticle item = mArrayList.get(position);

		holder.titleTextView.setText(item.Title);

		holder.mainItemLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				BaseFragmentActivity baseFragmentActivity = ((BaseFragmentActivity) mContext);
				if (item.Type == BeanCollectKnowledge.TYPE_ARTICLE_NEW) {

					if (item.Content.startsWith("http://")) {
						Intent intent = new Intent(mContext,
								MyArticleInfoActivity.class);
						intent.putExtra("url", item.Content);
						intent.putExtra("TargetID", item.ArticleID);
						baseFragmentActivity.startActivity(intent,
								true);
					} else {
						MyNetUtil.getInstance().requestArticleDetail(
								baseFragmentActivity,
								item.ArticleID,"","",item.Content);
					}
				} if (item.Type == BeanCollectKnowledge.TYPE_ARTICLE_WRITER) {

					//应该跳专家专栏文章详情
					MyNetUtil.getInstance().requestArticleDetail(
							baseFragmentActivity,
							item.ArticleID,"3","","");

				}else if (item.Type == BeanCollectKnowledge.TYPE_TIEZI) {
					Intent intent = new Intent(mContext,
							PubContentDetailActivity.class);
					intent.putExtra("TopicId", item.ArticleID);
					intent.putExtra("tag", 1);

					baseFragmentActivity.startActivity(intent,
							true);
				}

			}
		});

		return convertView;
	}

	private static class ViewHolder {

		LinearLayout mainItemLayout;
		TextView titleTextView;

	}
}
